import { Body, Controller, Delete, Get, HttpException, HttpStatus, Patch, Post, Query } from '@nestjs/common'
import { ApiTags } from '@nestjs/swagger'
import { CreateInvitationDto, FindOneInvitationDto, UpdateInvitationDto } from './dto/invitation.dto'
import { InvitationService } from './invitation.service'

@ApiTags('后台/二期/邀请奖励管理')
@Controller('admin/invitation')
export class InvitationController {
  constructor(private readonly invitationService: InvitationService) {}

  /**
   * 邀请奖励列表
   */
  @Get()
  list(@Query() query: FindOneInvitationDto) {
    return this.invitationService.findAll(query)
  }

  /**
   * 新增邀请奖励
   */
  @Post()
  create(@Body() data: CreateInvitationDto) {
    return this.invitationService.create(data)
  }

  /**
   * 更新邀请奖励
   */
  @Patch()
  async update(@Body() data: UpdateInvitationDto) {
    const findData = await this.invitationService.findOne({})
    if (!findData) {
      throw new HttpException('邀请奖励配置不存在', HttpStatus.NOT_FOUND)
    }
    await this.invitationService.update(findData.invitationId, data)
  }

  /**
   * 删除邀请奖励
   */
  @Delete()
  async delete(@Body() data: FindOneInvitationDto) {
    const findData = await this.invitationService.findOne(data)
    if (!findData) {
      throw new HttpException('邀请奖励配置不存在', HttpStatus.NOT_FOUND)
    }
    await this.invitationService.delete(findData.invitationId)
  }
}

@ApiTags('官网/二期/邀请奖励')
@Controller('web/invitation')
export class WebInvitationController {
  constructor(private readonly invitationService: InvitationService) {}

  /**
   * 获取邀请奖励
   */
  @Get()
  webList(@Query() query: FindOneInvitationDto) {
    return this.invitationService.findAll(query)
  }
}
